//
//  RadioViewController.swift
//  readtool
//
//  Created by Wan Jiading on 2017/6/7.
//  Copyright © 2017年 DAD. All rights reserved.
//

import UIKit

class RadioViewController: BaseViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

	@IBOutlet weak var collectionView: UICollectionView!
	
	/// leftMargin 或 rigntMargin
	var collectionViewHorizontalMargin: CGFloat!
	var collectionViewBottomMargin: CGFloat!
	
	var minimumLineSpacing: CGFloat!
	var minimumInteritemSpacing: CGFloat!
	
	var collectionHeaderView: RadioCollectionHeaderView?
	
	var dataModel: RadioHomeDataModel!
	
	/// 每一个 item 的 size
	var itemSize: CGSize!
	
    override func viewDidLoad() {
        super.viewDidLoad()

		setupDefaultData()
		setupControls()
    }
	
	func setupDefaultData() {
		
		setupTestData()
		
		if (IPHONE5) {
			
			itemSize = CGSize(width: 138, height: 66)
			collectionViewBottomMargin = 18
			collectionViewHorizontalMargin = 15
			
			minimumLineSpacing = 18
			minimumInteritemSpacing = 0
			
		} else if (IPHONE6) {
			
			itemSize = CGSize(width: 156, height: 66)
			collectionViewBottomMargin = 18
			collectionViewHorizontalMargin = 24
			
			minimumLineSpacing = 18
			minimumInteritemSpacing = 15
			
		}
		
	}
	
	func setupTestData() {
		
		
		dataModel = RadioHomeDataModel()
//		dataModel.desc = "点选宝贝喜欢的电台，立刻开始收听吧！点选宝贝喜欢的电台，立刻开始收听吧！"
		
		
		let radio1 = RadioModel()
		radio1.bg_color = "f3d3c3"
		radio1.icon_url = "http://dev.exploiter.ivydad.com.cn:80/static/home/images/categoryMoreFour/01_2x.png"
		radio1.id = "2"
		radio1.list_count = "45"
		radio1.title = "我下载的"
		radio1.type = "3"
		
		
		let radio = RadioModel()
		radio.bg_color = "e4d1f2"
		radio.icon_url = "http://dev.exploiter.ivydad.com.cn:80/static/home/images/categoryMoreFour/01_2x.png"
		radio.id = "2"
		radio.list_count = "66"
		radio.title = "我喜欢的"
		radio.type = "3"
		
		
		var radioList = [RadioModel]()
		radioList.append(radio)
		radioList.append(radio1)
		radioList.append(radio1)
		radioList.append(radio)
		
		let radioCategoryModel = RadioCategoryModel()
		radioCategoryModel.id = "1"
		radioCategoryModel.title = "我的"
		radioCategoryModel.radio_list = radioList
		
		var categoryList = [RadioCategoryModel]()
		categoryList.append(radioCategoryModel)
		categoryList.append(radioCategoryModel)
		categoryList.append(radioCategoryModel)
		categoryList.append(radioCategoryModel)
		
		dataModel.category_list = categoryList
	}
	
	func setupControls() {
	
		// 布局 item 间距
		let layout = UICollectionViewFlowLayout()
		layout.scrollDirection = .vertical
		layout.itemSize = itemSize
		layout.minimumLineSpacing = minimumLineSpacing
		layout.minimumInteritemSpacing = minimumInteritemSpacing

		collectionView.collectionViewLayout = layout
		collectionView.showsVerticalScrollIndicator = false
		collectionView.showsHorizontalScrollIndicator = false
		
		if let desc = dataModel.desc {
			collectionHeaderView = RadioCollectionHeaderView(title: desc)
			collectionView.addSubview(collectionHeaderView!)
			
			collectionHeaderView!.snp.makeConstraints { (make) in
				make.left.equalTo(self.collectionView.snp.left)
				make.right.equalTo(self.collectionView.snp.right)
				make.top.equalTo(self.collectionView.snp.top)
				make.width.equalTo(self.collectionView.snp.width)
			}
		}
		
		collectionView.layoutIfNeeded()
	}
	
	override func updateViewConstraints() {
		super.updateViewConstraints()
		
		if (collectionHeaderView != nil) {
			
			let headerViewHeight = collectionHeaderView!.getHeight()
			collectionHeaderView!.snp.remakeConstraints({ [unowned self] (make) in
				make.left.equalTo(self.collectionView.snp.left)
				make.top.equalTo(self.collectionView.snp.top).offset(-headerViewHeight)
				make.width.equalTo(self.collectionView.snp.width)
				make.height.equalTo(headerViewHeight)
			})
			
			collectionView.contentInset = UIEdgeInsets(top: 64 + headerViewHeight, left: 0, bottom: 0, right: 0)
		} else {
			collectionView.contentInset = UIEdgeInsets(top: 64, left: 0, bottom: 0, right: 0)
		}
	}
	
	func numberOfSections(in collectionView: UICollectionView) -> Int {
		if (dataModel == nil) {
			return 0
		}
		if (dataModel.category_list == nil) {
			JDToast.show("category_list为空")
			return 0
		}
		return dataModel.category_list!.count
	}
	
	func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
		return dataModel.category_list?[section].radio_list?.count ?? 0
	}
	
	/// 每一个 item
	public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
		let cell = collectionView.dequeueReusableCell(withReuseIdentifier: R.reuseIdentifier.radioCollectionViewCell.identifier, for: indexPath) as! RadioCollectionViewCell
		if let radioList = dataModel.category_list![indexPath.section].radio_list {
			cell.radio = radioList[indexPath.item]
		}
		return cell
	}
	
	
	/// section的内边距
	func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
		return UIEdgeInsets(top: 0,
		                    left: collectionViewHorizontalMargin,
		                    bottom: collectionViewBottomMargin,
		                    right: collectionViewHorizontalMargin)
	}
	
	
	// MARK: - section.header.hegiht
	
	/// section.header.hegiht
	func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
		if (section == 0) {
			return CGSize(width: 0, height: 60)
		} else {
			return CGSize(width: 0, height: 54)
		}
	}
	
	// MARK: - section.footer.hegiht
	
	/// section.footer.hegiht
	func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
		return CGSize(width: 0, height: 12)
	}
	
	// MARK: - 自定义 sectionHeaderView  sectionFooterView
	
	/// 设置 header 和 footer
	func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
		
		if (kind == UICollectionElementKindSectionHeader) {
			return setupCollectionView(collectionView, headerViewAt: indexPath)
		} else if (kind == UICollectionElementKindSectionFooter) {
			return setupCollectionView(collectionView, footerViewAt: indexPath)
		}
		
		return UICollectionReusableView()
	}

	func setupCollectionView(_ collectionView: UICollectionView, headerViewAt indexPath: IndexPath) -> UICollectionReusableView {
		let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: R.reuseIdentifier.radioCategoryHeaderView, for: indexPath)!
		headerView.title = dataModel.category_list![indexPath.section].title!
		
		return headerView
	}
	
	func setupCollectionView(_ collectionView: UICollectionView, footerViewAt indexPath: IndexPath) -> UICollectionReusableView {
		let footerView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionFooter, withReuseIdentifier: R.reuseIdentifier.radioCategoryFooterView, for: indexPath)!
		if (indexPath.section < dataModel.category_list!.count - 1) {
			footerView.backgroundColor = UIColor.rgb("f5f5f5")
		} else {
			footerView.backgroundColor = UIColor.white
		}
		return footerView
	}
	
}
